﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class getTreeData : System.Web.UI.Page
{
    //protected Whir.Model.Whir_AdminRole Role;
    protected string nodeNum = "";
    protected string columnIDs = Whir.BLL.Whir_AdminRole.getColumnIDsByRoleIDs(RoleHelper.AdminCache.roleIDs);
    protected bool bValue = (RoleHelper.AdminCache.typeID == 1) ? true : false;//是否超级管理员

    protected void Page_Load(object sender, EventArgs e)
    {
        if (RoleHelper.AdminCache != null)
        {
            string strTree1 = getTree("管理导航", "", "select columnID,columnName,(select url from Whir_Module where moduleMark=Whir_Column.moduleMark) as url,modelID,formID,param,operatePage from Whir_Column where parentID=", "0", " order by sort");

            //获取权限树
            Response.Clear();
            //Response.Charset = "utf-8";
            Response.Write(strTree1);
        }
        Response.End();
    }


    private string getTree(string title, string mark, string sql, string pid, string sqlOrder)
    {

        DataSet ds = Whir.SQLServerDAL.DbHelperSQL.Query(sql + pid + sqlOrder);
        if (ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0) return "";

        string nodeItem = "";
        bool btn = false;
        bool hasIndex = false;

        for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
        {
            nodeNum = ds.Tables[0].Rows[i][0].ToString();
            hasIndex = (columnIDs.IndexOf("," + ds.Tables[0].Rows[i][0].ToString() + ",") >= 0) ? true : false;
            if (hasIndex || bValue)
            {
                nodeItem += "{id:" + nodeNum.ToString() + ",text:'" + ds.Tables[0].Rows[i][1].ToString() + "',singleClickExpand:true";

                btn = BindNode(ref nodeItem, mark, sql, ds.Tables[0].Rows[i][0].ToString(), sqlOrder);
                if (!btn)
                {
                    string url = ds.Tables[0].Rows[i][2].ToString();
                    string param = ds.Tables[0].Rows[i][5].ToString();

                    url = "#";
                }
                nodeItem += "},";
            }
        }
        if (nodeItem.Length > 1)
        {
            nodeItem = nodeItem.Substring(0, nodeItem.Length - 1);
        }
        string strTree = "[" + nodeItem + "]";
        return strTree;
    }

    private bool BindNode(ref string nodeItem, string mark, string sql, string pid, string sqlOrder)
    {
        DataSet ds = null;
        ds = Whir.SQLServerDAL.DbHelperSQL.Query(sql + pid + sqlOrder);
        if (ds == null)
        {
            nodeItem += ",leaf:true";
            return false;
        }
        if (ds.Tables[0].Rows.Count == 0)
        {
            nodeItem += ",leaf:true";
            return false;
        }

        bool haveItem = true;
        bool btn = false;
        bool hasIndex = false;

        for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
        {
            nodeNum = ds.Tables[0].Rows[i][0].ToString();
            hasIndex = (columnIDs.IndexOf("," + ds.Tables[0].Rows[i][0].ToString() + ",") >= 0) ? true : false;
            if (hasIndex || bValue)
            {
                if (haveItem)
                {
                    haveItem = false;
                    nodeItem += ",children:[";
                }

                nodeItem += "{id:" + nodeNum.ToString() + ",text:'" + ds.Tables[0].Rows[i][1].ToString() + "',singleClickExpand:true";

                //nodeNum++;

                btn = BindNode(ref nodeItem, mark, sql, ds.Tables[0].Rows[i][0].ToString(), sqlOrder);

                if (!btn)
                {
                    string url = ds.Tables[0].Rows[i][2].ToString();
                    string param = ds.Tables[0].Rows[i][5].ToString();

                    url = "#";
                }
                nodeItem += "},";
            }
        }
        if (nodeItem.Substring(nodeItem.Length - 1, 1) == ",")
        {
            nodeItem = nodeItem.Substring(0, nodeItem.Length - 1);
        }
        if (haveItem)
        {
            nodeItem += ",leaf:true";
            nodeItem += ",href:'javascript:ezEIP.goTo();'";
        }
        else
        {
            nodeItem += "]";
        }
        return true;
    }
}
